package com.zmc.product.infrastructure.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmc.product.infrastructure.enums.ProductStatusEnum;
import com.zmc.common.utils.StringUtils;
import com.zmc.product.infrastructure.bean.vo.BasePropertyValueVo;
import com.zmc.product.infrastructure.service.CommodityCategoryPropertyValueService;
import com.zmc.product.infrastructure.bean.query.CommodityCategoryPropertyValueQuery;
import com.zmc.product.infrastructure.bean.vo.PropertyValueVo;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zmc.common.utils.PageUtils;

import com.zmc.product.infrastructure.dao.CommodityCategoryPropertyValueDao;
import com.zmc.product.infrastructure.entity.CommodityCategoryPropertyValueEntity;
import org.springframework.util.CollectionUtils;


/**
 * @author zmc
 */
@Service("commodityCategoryPropertyValueService")
public class CommodityCategoryPropertyValueServiceImpl extends ServiceImpl<CommodityCategoryPropertyValueDao, CommodityCategoryPropertyValueEntity> implements CommodityCategoryPropertyValueService {

    @Override
    public PageUtils pageCommodityCategoryPropertyValueList(CommodityCategoryPropertyValueQuery query) {
        Page<CommodityCategoryPropertyValueEntity> page = baseMapper.selectPage(new Page<>(query.getPageNum(), query.getPageSize()), getWrapper(query));
        return new PageUtils(page);
    }

    @Override
    public List<CommodityCategoryPropertyValueEntity> selectCommodityCategoryPropertyValueList(CommodityCategoryPropertyValueQuery query) {
        return list(getWrapper(query));
    }

    private LambdaQueryWrapper<CommodityCategoryPropertyValueEntity> getWrapper(CommodityCategoryPropertyValueQuery query) {
        LambdaQueryWrapper<CommodityCategoryPropertyValueEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CommodityCategoryPropertyValueEntity::getCategoryPropertyId, query.getCategoryPropertyId())
                .eq(StringUtils.isNotEmpty(query.getPropertyValue()), CommodityCategoryPropertyValueEntity::getPropertyValue, query.getPropertyValue())
                .orderByAsc(CommodityCategoryPropertyValueEntity::getOrderNum);
        return wrapper;
    }

    @Override
    public List<PropertyValueVo> getGoodsSpuPropertyValueListByCategoryIdAndType(Long categoryId) {
        LinkedList<PropertyValueVo> list = baseMapper.getGoodsSkuPropertyValueListByCategoryIdAndType(categoryId, ProductStatusEnum.CATEGORY_PROPERTY_TYPE_BASE.getCode());
        if (CollectionUtils.isEmpty(list)) {
            return list;
        }
        List<PropertyValueVo> res = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (i == 0 || !list.get(i).getPropertyName().equals(list.get(i -1).getPropertyName())) {
                res.add(new PropertyValueVo(list.get(i).getPropertyName(), null, "0"));
            }
            res.add(list.get(i));
        }
        return res;

    }

    @Override
    public List<PropertyValueVo> getGoodsSkuPropertyValueListByCategoryIdAndType(Long categoryId) {
        //sku属性
        return baseMapper.getGoodsSkuPropertyValueListByCategoryIdAndType(categoryId, ProductStatusEnum.CATEGORY_PROPERTY_TYPE_SALE.getCode());
    }

    @Override
    public Long countByCategoryId(Long categoryPropertyId) {
        LambdaQueryWrapper<CommodityCategoryPropertyValueEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CommodityCategoryPropertyValueEntity::getCategoryPropertyId, categoryPropertyId);
        return count(wrapper);
    }

    @Override
    public List<BasePropertyValueVo> getGoodsBaseInfoBySpuId(String spuId) {
        return baseMapper.getGoodsBaseInfoBySpuId(spuId);
    }

}